package io.renren.modules.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.demo.dao.DhGodsInfoDao;
import io.renren.modules.demo.dao.DhOutdorsportInfoDao;
import io.renren.modules.demo.dao.DhOutdorsportaddInfoDao;
import io.renren.modules.demo.dao.DhSportapplyInfoDao;
import io.renren.modules.demo.dto.ChildSixData;
import io.renren.modules.demo.dto.DhOutdorsportInfoDTO;
import io.renren.modules.demo.entity.DhOutdorsportInfoEntity;
import io.renren.modules.demo.entity.DhOutdorsportaddInfoEntity;
import io.renren.modules.demo.entity.DhSportapplyInfoEntity;
import io.renren.modules.demo.service.DhOutdorsportInfoService;
import io.renren.modules.sys.dao.SysUserDao;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-03-25
 */
@Service
public class DhOutdorsportInfoServiceImpl extends CrudServiceImpl<DhOutdorsportInfoDao, DhOutdorsportInfoEntity, DhOutdorsportInfoDTO> implements DhOutdorsportInfoService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private DhSportapplyInfoDao sportapplyInfoDao;

    @Autowired
    private DhOutdorsportInfoDao dhOutdorsportInfoDao;

    @Autowired
    private DhSportapplyInfoDao dhSportapplyInfoDao;

    @Autowired
    private DhOutdorsportaddInfoDao dhOutdorsportaddInfoDao;

    @Override
    public QueryWrapper<DhOutdorsportInfoEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<DhOutdorsportInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    /**
     * 活动报名
     *
     * @param sportId
     * @param userId
     * @return
     */
    @Override
    @Transactional
    public Integer sportApply(Integer sportId, Long userId) {
        int i;
        SysUserDTO sysUserDTO = sysUserService.getCLOUusername(userId);
        Integer username = Integer.valueOf(sysUserDTO.getUsername());
        List<DhSportapplyInfoEntity> infoBySportId = dhOutdorsportInfoDao.getInfoBySportId(sportId);
        boolean state = true;
        for (DhSportapplyInfoEntity dhSportapplyInfoEntity : infoBySportId) {
            if (dhSportapplyInfoEntity.getUserId().equals(username)) {
                state = false;
                break;
            }
        }
        if (state) {
            DhSportapplyInfoEntity dhSportapplyInfoEntity = new DhSportapplyInfoEntity();
            dhSportapplyInfoEntity.setSportId(sportId);
            dhSportapplyInfoEntity.setUserId(username);
            sportapplyInfoDao.insert(dhSportapplyInfoEntity);
            DhOutdorsportInfoEntity dhOutdorsportInfoEntity = dhOutdorsportInfoDao.selectById(sportId);
            Integer number = dhOutdorsportInfoEntity.getNumber();
            dhOutdorsportInfoEntity.setNumber(number + 1);
            LambdaQueryWrapper<DhOutdorsportInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select().eq(DhOutdorsportInfoEntity::getId, sportId);
            dhOutdorsportInfoDao.update(dhOutdorsportInfoEntity, queryWrapper);
            i = 0;
        } else {
            i = 1;
        }
        return i;
    }

    /**
     * 取消报名
     *
     * @param sportId
     * @param userId
     * @return
     */
    @Override
    @Transactional
    public Integer sportApplyCancel(Integer sportId, Long userId) {
        int i;
        SysUserDTO sysUserDTO = sysUserService.getCLOUusername(userId);
        Integer username = Integer.valueOf(sysUserDTO.getUsername());
        List<DhSportapplyInfoEntity> infoBySportId = dhOutdorsportInfoDao.getInfoBySportId(sportId);
        boolean state = false;
        for (DhSportapplyInfoEntity dhSportapplyInfoEntity : infoBySportId) {
            if (dhSportapplyInfoEntity.getUserId().equals(username)) {
                state = true;
                break;
            }
        }
        if (state) {
            dhSportapplyInfoDao.cancelApply(sportId, username);
            DhOutdorsportInfoEntity dhOutdorsportInfoEntity = dhOutdorsportInfoDao.selectById(sportId);
            Integer number = dhOutdorsportInfoEntity.getNumber();
            dhOutdorsportInfoEntity.setNumber(number - 1);
            LambdaQueryWrapper<DhOutdorsportInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select().eq(DhOutdorsportInfoEntity::getId, sportId);
            dhOutdorsportInfoDao.update(dhOutdorsportInfoEntity, queryWrapper);
            i = 0;
        } else {
            i = 1;
        }
        return i;
    }

    /**
     * 发起新的户外运动
     *
     * @param sportName
     * @param sportAddress
     * @param startTime
     * @param endTime
     * @param upper
     * @param sportLeader
     * @param traffic
     */
    @Override
    @Transactional
    public void newSport(String sportName, String sportAddress, String startTime, String endTime, Integer upper, String sportLeader, String traffic) {
        int sportId = (int) ((Math.random() * 9 + 1) * 10000000);
        DhOutdorsportInfoEntity dhOutdorsportInfoEntity = new DhOutdorsportInfoEntity();
        dhOutdorsportInfoEntity.setId(sportId);
        dhOutdorsportInfoEntity.setSportName(sportName);
        dhOutdorsportInfoEntity.setNumber(0);
        dhOutdorsportInfoEntity.setUpper(upper);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            Date newStartTime = simpleDateFormat.parse(startTime);
            Date newEndTime = simpleDateFormat.parse(endTime);
            dhOutdorsportInfoEntity.setStartTime(newStartTime);
            dhOutdorsportInfoEntity.setEndTime(newEndTime);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        dhOutdorsportInfoDao.insert(dhOutdorsportInfoEntity);
        DhOutdorsportaddInfoEntity dhOutdorsportaddInfoEntity = new DhOutdorsportaddInfoEntity();
        dhOutdorsportaddInfoEntity.setSportId(sportId);
        dhOutdorsportaddInfoEntity.setSportAddress(sportAddress);
        dhOutdorsportaddInfoEntity.setSportLeader(sportLeader);
        dhOutdorsportaddInfoEntity.setSportTraffic(traffic);
        dhOutdorsportaddInfoDao.insert(dhOutdorsportaddInfoEntity);
    }

    /**
     * 根据户外运动编号获取运动信息
     *
     * @param sportId
     * @return
     */
    @Override
    public DhOutdorsportInfoDTO getSportInfo(Integer sportId) {
        DhOutdorsportInfoEntity dhOutdorsportInfoEntity = dhOutdorsportInfoDao.selectById(sportId);
        DhOutdorsportInfoDTO dhOutdorsportInfoDTO = new DhOutdorsportInfoDTO();
        dhOutdorsportInfoDTO.setSportName(dhOutdorsportInfoEntity.getSportName());
        dhOutdorsportInfoDTO.setStartTime(dhOutdorsportInfoEntity.getStartTime());
        dhOutdorsportInfoDTO.setEndTime(dhOutdorsportInfoEntity.getEndTime());
        return dhOutdorsportInfoDTO;
    }

    /**
     * 获取不同活动报名人数
     *
     * @return
     */
    @Override
    public List<ChildSixData> getChildSixData() {
        return dhOutdorsportInfoDao.getChildSixData();
    }
}